﻿@page "/tests/datagrid/selection/multiple"
<Row>
    <Column>
        <Card Margin="Margin.Is4.OnY">
            <CardHeader>
                <CardTitle>Datagrid: Multiple Selection</CardTitle>
            </CardHeader>
            <CardBody>
                <Paragraph>Set the DataGrid to multiple selection by setting the <code></code> to <code>Multiple</code>.</Paragraph>
                <UnorderedList>
                    <UnorderedListItem>
                        Set <code>DataGridMultiSelectColumn</code> for checkbox support.
                    </UnorderedListItem>
                    <UnorderedListItem>
                        Bind the selected rows to a property by binding to the <code>SelectedRows</code> parameter.
                    </UnorderedListItem>
                    <UnorderedListItem>
                        Select various rows by pressing <kbd>Shift</kbd> + <kbd>Click</kbd>.
                    </UnorderedListItem>
                </UnorderedList>
            </CardBody>
            <CardBody>
                <DataGrid TItem="Employee"
                          Data="inMemoryData"
                          SelectionMode="DataGridSelectionMode.Multiple"
                          @bind-SelectedRows="selectedEmployees"
                          Responsive
                          ShowPager
                          ShowPageSizes>
                    <DataGridColumns>
                        <DataGridMultiSelectColumn TItem="Employee"></DataGridMultiSelectColumn>
                        <DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name">
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City">
                            <CaptionTemplate>
                                <Icon Name="IconName.City" /> @context.Caption
                            </CaptionTemplate>
                        </DataGridColumn>
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Zip )" Caption="Zip">
                        </DataGridColumn>
                        <DataGridDateColumn TItem="Employee" Field="@nameof( Employee.DateOfBirth )" DisplayFormat="{0:dd.MM.yyyy}" Caption="Date Of Birth" Editable />
                        <DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" />
                        <DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable Data="EmployeeData.Genders" ValueField="(x) => ((Gender)x).Code" TextField="(x) => ((Gender)x).Description" />
                        <DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End">
                        </DataGridColumn>
                        <DataGridCheckColumn TItem="Employee" Field="@nameof(Employee.IsActive)" Caption="Active" Editable Filterable="false">
                            <DisplayTemplate>
                                <Check TValue="bool" Checked="context.IsActive" Disabled ReadOnly />
                            </DisplayTemplate>
                        </DataGridCheckColumn>
                    </DataGridColumns>
                </DataGrid>
            </CardBody>
        </Card>
    </Column>
</Row>

@if ( selectedEmployees?.Count > 0 )
{
    @if ( selectedEmployees?.Count <= 5 )
    {
        @foreach ( var employee in selectedEmployees )
        {
            <Row @key="employee" Margin="Margin.Is1.FromTop">
                <Column>
                    <Card>
                        <CardHeader>
                            <CardTitle>Employee</CardTitle>
                        </CardHeader>
                        <CardBody>
                            <Fields>
                                <Field>
                                    <FieldLabel>First Name</FieldLabel>
                                    <FieldBody>
                                        <TextEdit ReadOnly Text="@employee?.FirstName"></TextEdit>
                                    </FieldBody>
                                </Field>
                                <Field>
                                    <FieldLabel>Last Name</FieldLabel>
                                    <TextEdit ReadOnly Text="@employee?.LastName"></TextEdit>
                                </Field>
                            </Fields>
                            <Fields>
                                <Field>
                                    <FieldLabel>Email</FieldLabel>
                                    <FieldBody>
                                        <TextEdit ReadOnly Text="@employee?.Email"></TextEdit>
                                    </FieldBody>
                                </Field>
                                <Field>
                                    <FieldLabel>City</FieldLabel>
                                    <TextEdit ReadOnly Text="@employee?.City"></TextEdit>
                                </Field>
                            </Fields>
                        </CardBody>
                    </Card>
                </Column>
            </Row>
        }
    }
    else
    {
        <Row Margin="Margin.Is1.FromTop">
            <Column>
                <Card>
                    <CardHeader>
                        <CardTitle>Selected Employees</CardTitle>
                    </CardHeader>
                    <CardBody>
                        <Fields>
                            <Field>
                                <FieldLabel>Quantity</FieldLabel>
                                <FieldBody>
                                    <TextEdit ReadOnly Text="@selectedEmployees.Count.ToString()"></TextEdit>
                                </FieldBody>
                            </Field>
                        </Fields>
                    </CardBody>
                </Card>
            </Column>
        </Row>
    }
}

@code {

    [Inject] EmployeeData EmployeeData { get; set; }

    private IEnumerable<Employee> inMemoryData;
    private List<Employee> selectedEmployees;

    protected override async Task OnInitializedAsync()
    {
        inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 );
        await base.OnInitializedAsync();
    }
}